package com.tian.controller;

import com.tian.common.CommonResult;
import com.tian.entity.ChargingData;
import com.tian.entity.ChargingDataRespDto;
import com.tian.enums.ResultCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import plus.ojbk.influxdb.core.InfluxdbTemplate;
import plus.ojbk.influxdb.core.Op;
import plus.ojbk.influxdb.core.Query;
import plus.ojbk.influxdb.core.enums.Order;
import plus.ojbk.influxdb.core.model.QueryModel;
import plus.ojbk.influxdb.util.InfluxdbUtils;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;

/**
 * {@code @description:} TODO
 *
 * @author tianwc 公众号：Java后端技术全栈
 * 在线刷题 1200+java面试题和1000+篇技术文章：<a href="https://woaijava.cc/">博客地址</a>
 * {@code @date:} 2024-08-23 21:55
 * {@code @version:} 1.0
 */
@RestController
@RequestMapping("/test")
public class TestController {
    @Autowired
    private InfluxdbTemplate influxDBTemplate;
    @RequestMapping("/teaddst")
    public String add() {
        ChargingData chargingData1 = new ChargingData();
        chargingData1.setChargeNum(new BigDecimal("100"));
        chargingData1.setGunNo("123456");
        chargingData1.setChargeRecordId("10001");
        chargingData1.setUserId(10001L);
        chargingData1.setTime(LocalDateTime.now());
        influxDBTemplate.insert(chargingData1);
        return "success";
    }
    @RequestMapping("/test")
    public String test() {
        QueryModel queryModel = new QueryModel();
        Map<String, Object> map = new TreeMap<>();
        map.put("gun_no", "123456");
        map.put("charge_record_id", "10001");
        queryModel.setMeasurement(InfluxdbUtils.getMeasurement(ChargingData.class));
        queryModel.setMap(map);
        queryModel.setStart(LocalDateTime.now().minusMinutes(100));
        queryModel.setEnd(LocalDateTime.now());
        queryModel.setOrder(Order.DESC);
        queryModel.setWhere(Op.where(queryModel) + " LIMIT 1");
        queryModel.setWhere(Op.where(queryModel));
        List<ChargingData> chargingDataList = influxDBTemplate.selectList(Query.build(queryModel), ChargingData.class);
        if (chargingDataList.isEmpty()) {
            return "not found";
        }
        for (ChargingData chargingData : chargingDataList) {
            System.out.println(chargingData);
        }
        return "success";
    }
}
